/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import io.katharsis.queryParams.QueryParams;
import io.katharsis.repository.annotations.JsonApiFindAll;
import io.katharsis.repository.annotations.JsonApiResourceRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.repoDomain.tmodel.LocationInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(LocationInfo.class)
@Component
public class LocationInfoRepository extends BaseRepository<LocationInfo> {

	@Autowired
	protected CustomerService customerService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(LocationInfo.class);
	}

	@Override
	@JsonApiFindAll
	public Iterable<LocationInfo> findAll(final QueryParams requestParams) {
		List dataList = new ArrayList<>();
		Map<String, String> map = CommonUtil.getKeyMapByFilter(requestParams);
		if (map.containsKey("bid")) {//老人位置信息
			long bid = -1;
			if (map.get("bid") != null && !"".equals(map.get("bid"))) {
				bid = Long.parseLong(map.get("bid"));
			}
			return customerService.getCustomerLocationData(ActionContextHolder.getContext().getTenantId(), bid);
		}

		if (map.containsKey("ebid")) {//员工位置信息
			long ebid = -1;
			if (map.get("ebid") != null && !"".equals(map.get("ebid"))) {
				ebid = Long.parseLong(map.get("ebid"));
			}
			return customerService.getStaffLocationData(ActionContextHolder.getContext().getTenantId(), ebid);
		}
		return dataList;

	}

}
